import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:get/get.dart';
import 'package:hj_crm/http/request_repository.dart';
import 'package:hj_crm/utils/form_utils.dart';

/// 参会记录表单
class DistributorConferenceForm extends StatefulWidget {
  final String? uId;

  const DistributorConferenceForm({super.key, this.uId});

  @override
  State<DistributorConferenceForm> createState() => _DistributorConferenceFormState();
}

class _DistributorConferenceFormState extends State<DistributorConferenceForm> {
  RequestRepository repository = Get.find<RequestRepository>();

  final _formKey = GlobalKey<FormBuilderState>();

  late TextEditingController _addressController;
  late TextEditingController _timeController;

  @override
  void initState() {
    super.initState();
    _addressController = TextEditingController();
    _timeController = TextEditingController();
  }

  @override
  void dispose() {
    _addressController.dispose();
    _timeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),
      child: Scaffold(
        appBar: AppBar(
          title: const Text('参会记录'),
        ),
        body: SingleChildScrollView(
          child: FormBuilder(
            key: _formKey,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  FormBuilderTextField(
                    name: 'address',
                    decoration: const InputDecoration(labelText: '会议地点'),
                    validator: FormBuilderValidators.compose([
                      FormBuilderValidators.required(errorText: '请输入会议地点'),
                    ]),
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.next,
                    controller: _addressController,
                  ),
                  FormBuilderTextField(
                      name: 'time',
                      decoration: const InputDecoration(labelText: '参会时间'),
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(errorText: '请选择参会时间'),
                      ]),
                      keyboardType: TextInputType.datetime,
                      controller: _timeController,
                      textInputAction: TextInputAction.done,
                      readOnly: true,
                      onTap: () {
                        openDatePicker(context, DateItemKind.conferenceDate, (value) {
                          setState(() {
                            _timeController.text = value;
                          });
                        });
                      }),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      //{customerInfoId: "1877553049627545600", conferenceAddress: "2222", conferenceTime: "2025-1-8 0:0:0"}
                      /// 验证表单
                      if (!_formKey.currentState!.saveAndValidate()) {
                        return;
                      }
                      Map<String, dynamic> map = {
                        "customerInfoId": widget.uId,
                        "conferenceAddress": _addressController.text,
                        "conferenceTime": _timeController.text + ' 00:00:00',
                      };
                      repository.addDistributorConference(
                          params: map,
                          success: (data) {
                            Navigator.pop(context, true);
                          },
                          fail: (code, msg) {});
                    },
                    child: const Text('保存'),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
